package com.nstar.helloworld.view
{
	import mx.containers.VBox;
	import com.nstar.base.controls.ButtonFX;
	import mx.events.FlexEvent;
	import flash.events.MouseEvent;
	import com.nstar.helloworld.events.QueryEvent;
	import mx.controls.TextInput;
	import mx.controls.Alert;
	import com.nstar.base.controls.TurnPage;
	import com.nstar.helloworld.model.Model;
	import mx.controls.DataGrid;
	import flash.events.Event;
	import mx.collections.ArrayCollection;
	import com.nstar.base.controls.AnimatedTileList;
	import com.nstar.helloworld.mxml.UserInfoView;
	import mx.controls.Label;
	import com.nstar.base.util.Logger;
	import mx.utils.ObjectUtil;
	import com.nstar.helloworld.events.QueryByIdEvent;

	public class QueryClass extends VBox
	{
		public var btnQuery:ButtonFX;
		public var txtUserId:TextInput;
		public var turnPage:TurnPage;
		public var grid:DataGrid;
		public var boxResult:VBox;
		public var atlResult:AnimatedTileList;
		public var lblInfo:Label;
		public var boxDesc:VBox;
		
		//模型
		var model:Model = Model.getInstance();
		var logger:Logger = Logger.getInstance();
		
		
		
		public function QueryClass()
		{
			//TODO: implement function
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		function init(event:FlexEvent):void{
			btnQuery.addEventListener(MouseEvent.CLICK,queryHandler);
			atlResult.addEventListener("userDesc",userDescHandler);
			boxDesc.addEventListener("tile",tileHandler);
		}
		
		function queryHandler(event:MouseEvent):void{
			var request:Object = new Object();
			request.userId = txtUserId.text;
			request.pagenum = 1;
			if (txtUserId.text != ""){
				var queryByIdEvent:QueryByIdEvent = new QueryByIdEvent(QueryByIdEvent.QUERY,request);
				this.dispatchEvent(queryByIdEvent);
			}else{
				var queryEvent:QueryEvent = new QueryEvent(QueryEvent.QUERY,request);
				this.dispatchEvent(queryEvent);
				model.queryRequest = request;
			}
			
		}
		
		//翻页组件的浏览事件
		public function browseHandler(event:Event):void{
			var queryEvent:QueryEvent = 
				new QueryEvent(QueryEvent.QUERY,model.queryRequest);
			this.dispatchEvent(queryEvent);
			
		}
		
		//刷新数据
		public function refresh(result:ArrayCollection):void{
			//grid.dataProvider = result;
			//turnPage.request = model.queryRequest;
			boxResult.visible = true;
			boxResult.width = 600;
			atlResult.width = 600;
			atlResult.dataProvider = result;
			lblInfo.text = "共 "+result.length+" 条记录。";
		}		
		
		//显示用户详细信息
		function userDescHandler(event:Event):void{
			atlResult.width = 0;
			boxDesc.visible = true;
			boxDesc.percentWidth = 100;
		}
		
		function tileHandler(event:Event):void{
			atlResult.percentWidth = 100;
			boxDesc.width = 0;
		}
		
		
	}
}